<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <template #header>
          <el-row>
            <el-col :span="20">
              <span>{{ compony.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDep()">
                        添加子部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <!-- card body -->
        <el-tree
          :data="departdata"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ compony.manager || "---" }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDep(data, 1)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="addDep(data, 2)">
                          编辑部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="delDEpart(data)">
                          删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <Dialog
      ref="popsheet"
      :depts="depts"
      :showdialog.sync="showPop"
      :currentpart="currentPart"
      @update-depart="getComponyDepart"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/table'
import { getDepart } from '@/utils'
import Dialog from './components/addDepartPop.vue'
export default {
  components: {
    Dialog
  },
  data () {
    return {
      depts: [],
      departdata: [
        {
          name: '总裁办'
        },
        {
          name: '财务部',
          children: [{
            name: '税务'
          }]
        }
      ],
      compony: { name: '', manager: '' },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showPop: false,
      currentPart: null
    }
  },
  created () {
    this.getComponyDepart()
  },
  methods: {
    addDep (data, type) {
      if (type === 1) { console.log('添加部门') }

      this.showPop = true
      this.currentPart = data
      if (type === 2) {
        this.$refs.popsheet.getDetail(data.id)
      }
    },
    async delDEpart (data) {
      if (data.children && data.children.length !== 0) return this.$message.error('该部门下尚有子部门,不能删除')
      try {
        await this.$confirm(`确定删除${data.name}吗`, '提示')
        await delDepartments(data.id)
        this.$message.success('删除成功')
        this.getComponyDepart()
      } catch (error) {
        console.log(error)
      }
    },
    async getComponyDepart () {
      const res = await getDepartments()
      this.compony.name = res.companyName
      this.compony.manager = res.companyManage
      this.departdata = getDepart(res.depts)
      this.depts = res.depts
    },
    handleNodeClick (data) {
      console.log(data)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
